<template>
  <GForm ref="detail" inline :config="inputConfig" :rules="rules" v-model="detailData"></GForm>
</template>

<script>
export default {
  name: 'Detail',
  data() {
    return {
      newFlag: true,
      detailData: {},
      compantList: []
    }
  },
  computed: {
    inputConfig() {
      return [
        {
          type: 'input',
          label: this.$t('common.user'),
          prop: 'username',
          disabled: !this.newFlag
        },
        {
          type: 'input',
          label: this.$t('common.pwd'),
          inputType: 'password',
          prop: 'password',
          show: this.newFlag
        },
        {
          type: 'input',
          label: this.$t('app.fullname'),
          prop: 'fullname'
        },
        {
          type: 'input',
          label: this.$t('app.email'),
          prop: 'email'
        },
        {
          type: 'select',
          label: '所属公司',
          prop: 'compantId',
          options: this.compantList
        }
      ]
    },
    rules() {
      return {
        username: [{ required: true, trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur' }],
        fullname: [{ required: true, trigger: 'blur' }]
      }
    }
  },
  methods: {
    async getData() {
      try {
        let res = await this.$api.get(`/userQuery/${this.selectData.username}`)

        this.detailData = res.bean
      } catch (e) {
        this.$message.error(e)
      }
    },
    getOptions() {
      this.$post('/v1/edi/plCompany/selectPlCompany', {
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        const data = res.data.item
        this.compantList = data.map(i => {
          i.label = i.companyName
          i.value = i.id
          return i
        })
      })
    }
  },
  mounted() {
    this.getOptions()
    this.newFlag = this.$store.state.newFlag
    //判断是否事新建
    if (Object.keys(this.$store.state.skip).length !== 0) {
      let data = this.$store.state.skip
      this.detailData = data.detail
      this.selectData = data.routeData
      this.detailNav = data.detailNav
    } else if (this.newFlag) {
      this.detailData = {
        status: 1,
        locked: 1,
        userLanguage: 'zh'
      }
    } else {
      this.selectData = this.$store.state.routeData
      this.getData()
    }
  }
}
</script>

<style scoped>
</style>
